#!/usr/bin/perl
use strict;
use Encode;

#http://opendata.baidu.com/api.php?query=183.14.110.7&co=&resource_id=6006&t=1354984168100&ie=utf8&oe=gbk


sub main {
	foreach my $a (1 .. 250) {
		foreach my $b (1 .. 250) {
			my $ip = "183." . $a . "." . $b . ".7";
			my $cmd = "curl -v \"http://opendata.baidu.com/api.php?query=$ip&co=&resource_id=6006&t=&ie=utf8&oe=gbk\" 2>>/dev/null";
			my $content = `$cmd`;
			#print "$content\n";
			my $locate;
			my $qip;
			if ($content =~ /"location":"(.+)", "titlecont":/)	{
				$locate = $1;
			}
			if ($content =~ /"origipquery":"(.+)", "showlamp"/) {
				$qip = $1;
			}
			print "$qip, $locate\n";
			insert_table($qip, $locate);
		}
	}
}

sub insert_table {
my ($qip, $locate) = @_;
$locate =  encode("utf-8", decode("gbk",$locate));
	system("mysql -h192.168.56.101 -uroot -p123456 test -e \"insert into t_ip  (ip,location) values ('$qip','$locate')\"");
}

main();
